<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/6/8
 * Time: 22:07
 */

namespace app\Module\Index\Controller;


use app\Module\Index\Logic\MessageLogic;
use app\Module\Index\Logic\MessageType;
use app\Module\Index\Model\CircleCollectModel;
use app\Module\Index\Model\CircleModel;
use app\Module\Index\Model\CircleTypeModel;
use app\Module\Index\Model\LogCircleLookModel;

class CircleCollectController extends CheckAuth
{
    /**
     * 切换关注状态
     *
     * @verify int $circle_id
     * @return string
     */
    public function toggleCollectAction($circle_id)
    {
        $circleCollectModel = new CircleCollectModel();
        $data               = $circleCollectModel->row([
            'user_id'   => $this->userId,
            'circle_id' => $circle_id,
        ]);

        $circleModel = new CircleModel();
        $circleData  = $circleModel->row([
            'id' => $circle_id,
        ]);

        if (empty($data)) {
            $circleCollectModel->insert([
                'user_id'   => $this->userId,
                'circle_id' => $circle_id,
                'created'   => now(),
            ]);
            $circleModel->update('collect_num =collect_num+1', [
                'id' => $circle_id,
            ]);

            $messageLogic = new MessageLogic();
            $messageLogic->add($this->userId, $circleData['user_id'], $circle_id, MessageType::CIRCLE_COLLECT, '');

            return success(true);
        } else {
            $circleCollectModel->delete([
                'user_id'   => $this->userId,
                'circle_id' => $circle_id,
            ]);
            $circleModel->update('collect_num =collect_num-1', [
                'id' => $circle_id,
            ]);

            $messageLogic = new MessageLogic();
            $messageLogic->delete($this->userId, $circleData['user_id'], $circle_id, MessageType::CIRCLE_COLLECT);

            return success(false);
        }
    }

    /**
     * 获取关注状态
     * @verify int $circle_id
     * @return bool
     */
    public function getCollectAction($circle_id)
    {
        $circleCollectModel = new CircleCollectModel();
        $data               = $circleCollectModel->row([
            'user_id'   => $this->userId,
            'circle_id' => $circle_id,
        ]);

        return success(!empty($data));
    }

    /**
     * 添加圈子浏览量
     * @verify int $circle_id
     * @return string
     */
    public function addLookNumAction($circle_id)
    {
        $logCircleLookModel = new LogCircleLookModel();
        $logCircleLookModel->addWhere('created', date("Y-m-d", strtotime("-1 hour")), '>=');
        $logCircleLookModel->addWhere([
            'circle_id' => $circle_id,
            'user_id'   => $this->userId,
        ]);
        $lookNum = $logCircleLookModel->count('1', null, 600);
        // 每个用户每小时只能增加一个
        if ($lookNum > 0) {
            return success();
        }

        $logCircleLookModel->insert([
            'circle_id' => $circle_id,
            'user_id'   => $this->userId,
            'created'   => now(),
        ]);
        $circleModel = new CircleModel();
        $circleModel->update('look_num=look_num+1', ['id' => $circle_id]);
        return success();
    }

    /**
     * 获取收藏圈子列表
     *
     * @param int $page
     * @param int $limit
     * @return string
     */
    public function getListAction($page = 1, $limit = 20)
    {
        $circleModel = new CircleModel();
        $circleModel->setFields([
            'circle.id',
            'circle.logo',
            'circle.name',
            'circle.desc',
            'circle.account_type',
            'circle.slideshow',
            'user.id as user_id',
            'user.address_name',
            'user.address',
            'user.address',
            'user.latitude',
            'user.longitude',
            'user.mobile',
        ]);
        $circleModel->setPage($page, $limit);
        $circleModel->addWhere([
            'audit'                  => 'pass',
            'circle_collect.user_id' => $this->userId,
        ]);
        $circleModel->setJoin('user', 'user.circle_id = circle.id');
        $circleModel->setJoin('circle_collect', 'circle_collect.circle_id = circle.id');
        $circleModel->addOrder('circle_collect.created', false);
        $data = $circleModel->select([]);
        $ids  = [];
        foreach ($data as $datum) {
            $ids[] = $datum['id'];
        }

        if (!empty($ids)) {
            $circleTypeModel = new CircleTypeModel();
            $circleTypeModel->setJoin('circle_one_type', 'circle_one_type.id=circle_type.one_type_id');
            $circleTypeModel->setJoin('circle_two_type', 'circle_two_type.id=circle_type.two_type_id');
            $circleTypeModel->addWhere('circle_id', $ids);
            $circleTypeModel->setFields('circle_id,circle_one_type.id as one_type_id,GROUP_CONCAT(DISTINCT circle_two_type.id) as two_type_id,circle_one_type.name as one_type_name,GROUP_CONCAT(DISTINCT circle_two_type.name) as two_type_name');
            $circleTypeModel->addGroup(['circle_id', 'circle_one_type.id']);
            $dataType = $circleTypeModel->select([], 600);
        } else {
            $dataType = [];
        }

        $dataType_ = [];
        foreach ($dataType as $item) {
            if (!isset($dataType_[$item['circle_id']])) {
                $dataType_[$item['circle_id']] = [];
            }
            $dataType_[$item['circle_id']][] = [
                'one_type_id'   => $item['one_type_id'],
                'two_type_id'   => $item['two_type_id'],
                'one_type_name' => $item['one_type_name'],
                'two_type_name' => $item['two_type_name'],
            ];
        }

        foreach ($data as $key => $datum) {
            if (isset($dataType_[$datum['id']])) {
                $datum['type'] = $dataType_[$datum['id']];
            } else {
                $datum['type'] = [];
            }
            $data[$key] = $datum;
        }

        return success($data);
    }
}